import Vue from "vue"
export default {
	state: {
		cartList: [
			{
				'count': 1,
				'selected': true,
				"promId": 0,
				"showPoints": false,
				"itemTagList": [{
					"itemId": 1535004,
					"tagId": 128111157,
					"freshmanExclusive": false,
					"name": "暖冬特惠",
					"subType": 204,
					"forbidJump": false,
					"type": 2
				}],
				"rank": 1,
				"id": 1535004,
				"sellVolume": 4001,
				"primaryPicUrl": "http://img.jianke.com/mall/product/202202/3b3237adf22c47d89940dace5d143e21.jpg",
				"soldOut": false,
				"sortFlag": 0,
				"commentCount": 0,
				"onSaleTime": 1538101761748,
				"picMode": 1,
				"commentWithPicCount": 0,
				"underShelf": false,
				"status": 2,
				"couponConflict": true,
				"forbiddenBuy": false,
				"promotionDesc": "暖冬特惠",
				"limitedFlag": 204,
				"pieceNum": 0,
				"itemSizeTableDetailFlag": false,
				"forbidExclusiveCal": false,
				"rewardShareFlag": false,
				"updateTime": 1575893634989,
				"showCommentEntrance": true,
				"pieceUnitDesc": "件",
				"specialPromTag": "",
				"counterPrice": 299,
				"categoryL2Id": 0,
				"retailPrice": 1700/100,
				"primarySkuPreSellPrice": 0,
				"preLimitFlag": 0,
				"itemPromValid": true,
				"promTag": "暖冬特惠",
				"source": 0,
				"points": 0,
				"primarySkuPreSellStatus": 0,
				"extraServiceFlag": 0,
				"flashPageLink": "",
				"autoOnsaleTimeLeft": 0,
				"innerData": {},
				"saleCenterSkuId": 0,
				"pointsStatus": 0,
				"extraPrice": "",
				"colorNum": 0,
				"showTime": 0,
				"autoOnsaleTime": 0,
				"preemptionStatus": 1,
				"isPreemption": 0,
				"zcSearchFlag": false,
				"name":"苯磺酸氨氯地平片(海迈平)(仁和)",
				"appExclusiveFlag": false,
				"itemType": 1,
				"listPicUrl": "https://yanxuan-item.nosdn.127.net/c2eeb1b872af1b8efc179a7515aacdaa.png",
				"pointsPrice": 0,
				"simpleDesc": "色拉姆发热面料，加厚升级",
				"seoTitle": "",
				"newItemFlag": false,
				"buttonType": 0,
				"primarySkuId": 1636062,
				"displaySkuId": 1636056,
				"productPlace": "",
				"itemSizeTableFlag": false
			},
			{
				'count': 1,
				'selected': false,
				"promId": 0,
				"showPoints": false,
				"itemTagList": [{
					"itemId": 1536001,
					"tagId": 128111157,
					"freshmanExclusive": false,
					"name": "暖冬特惠",
					"subType": 204,
					"forbidJump": false,
					"type": 2
				}],
				"rank": 1,
				"id": 1536001,
				"sellVolume": 3634,
				"primaryPicUrl": "http://img.jianke.com/mall/product/202003/c76e8111bf4f477ea384250ed512f1ba.jpg",
				"soldOut": false,
				"sortFlag": 0,
				"commentCount": 0,
				"onSaleTime": 1538101896296,
				"picMode": 1,
				"commentWithPicCount": 0,
				"underShelf": false,
				"status": 2,
				"couponConflict": true,
				"forbiddenBuy": false,
				"promotionDesc": "暖冬特惠",
				"limitedFlag": 204,
				"pieceNum": 0,
				"itemSizeTableDetailFlag": false,
				"forbidExclusiveCal": false,
				"rewardShareFlag": false,
				"updateTime": 1575894115275,
				"showCommentEntrance": true,
				"pieceUnitDesc": "件",
				"specialPromTag": "",
				"counterPrice": 299,
				"categoryL2Id": 0,
				"retailPrice":950/100,
				"primarySkuPreSellPrice": 0,
				"preLimitFlag": 0,
				"itemPromValid": true,
				"promTag": "暖冬特惠",
				"source": 0,
				"points": 0,
				"primarySkuPreSellStatus": 0,
				"extraServiceFlag": 0,
				"flashPageLink": "",
				"autoOnsaleTimeLeft": 0,
				"innerData": {},
				"saleCenterSkuId": 0,
				"pointsStatus": 0,
				"extraPrice": "",
				"colorNum": 0,
				"showTime": 0,
				"autoOnsaleTime": 0,
				"preemptionStatus": 1,
				"isPreemption": 0,
				"zcSearchFlag": false,
				"name": "胸腺肽肠溶片(迪尔舒)",
				"appExclusiveFlag": false,
				"itemType": 1,
				"listPicUrl": "https://yanxuan-item.nosdn.127.net/02b61fb5700aed6761b7524d98ed0837.png",
				"pointsPrice": 0,
				"simpleDesc": "色拉姆发热面料，加厚升级",
				"seoTitle": "",
				"newItemFlag": false,
				"buttonType": 0,
				"primarySkuId": 1634105,
				"displaySkuId": 1634104,
				"productPlace": "",
				"itemSizeTableFlag": false
			},
			{
				'selected': false,
				'count': 2,
				"promId": 0,
				"showPoints": false,
				"itemTagList": [{
					"itemId": 1562007,
					"tagId": 128111157,
					"freshmanExclusive": false,
					"name": "暖冬特惠",
					"subType": 204,
					"forbidJump": false,
					"type": 2
				}],
				"rank": 1,
				"id": 1562007,
				"sellVolume": 2477,
				"primaryPicUrl":"http://img.jianke.com/mall/product/202111/d289b13c613244b6afcc9b73c5619b27.jpg",
				"soldOut": false,
				"sortFlag": 0,
				"commentCount": 0,
				"onSaleTime": 1536637196215,
				"picMode": 1,
				"commentWithPicCount": 0,
				"underShelf": false,
				"status": 2,
				"couponConflict": true,
				"forbiddenBuy": false,
				"promotionDesc": "暖冬特惠",
				"limitedFlag": 204,
				"pieceNum": 0,
				"itemSizeTableDetailFlag": false,
				"forbidExclusiveCal": false,
				"rewardShareFlag": false,
				"updateTime": 1576742994144,
				"showCommentEntrance": true,
				"pieceUnitDesc": "件",
				"specialPromTag": "",
				"counterPrice": 299,
				"categoryL2Id": 0,
				"retailPrice":  580/100,
				"primarySkuPreSellPrice": 0,
				"preLimitFlag": 0,
				"itemPromValid": true,
				"promTag": "暖冬特惠",
				"source": 0,
				"points": 0,
				"primarySkuPreSellStatus": 0,
				"extraServiceFlag": 0,
				"flashPageLink": "",
				"autoOnsaleTimeLeft": 0,
				"innerData": {},
				"saleCenterSkuId": 0,
				"pointsStatus": 0,
				"extraPrice": "",
				"colorNum": 4,
				"showTime": 0,
				"autoOnsaleTime": 0,
				"preemptionStatus": 1,
				"isPreemption": 0,
				"zcSearchFlag": false,
				"name":  "恩替卡韦分散片(和恩)",
				"appExclusiveFlag": false,
				"itemType": 1,
				"listPicUrl": "https://yanxuan-item.nosdn.127.net/bb4c991ad08c0b061c083cd7b2f2dd08.png",
				"pointsPrice": 0,
				"simpleDesc": "轻薄打底告别臃肿",
				"seoTitle": "",
				"newItemFlag": false,
				"buttonType": 0,
				"primarySkuId": 1623203,
				"displaySkuId": 1623197,
				"productPlace": "",
				"itemSizeTableFlag": false
			},
		]
	},
	mutations: {
		//修改选中状态
		changeSelected(state, {
			selected,
			index
		}) {
			state.cartList[index].selected = selected
		},
		// 修改数量
		changeNum(state, {
			type,
			index
		}) {
			if (type) {
				state.cartList[index].count++
				return
			}
			if (state.cartList[index].count > 1) {
				state.cartList[index].count--
				return
			}
			uni.showModal({
				content:'确定要删除吗',
				success: (res) => {
					if(res.confirm){
						state.cartList.splice(index, 1)
					}
				}
			})
			
		},
		// 修改全选
		changeAllSelected(state, selected) {
			state.cartList.forEach(item => item.selected = selected)
		},
		changeCartList(state, shopItem) {
			let cateObj = state.cartList.find(item=>item.name == shopItem.name)
			if (cateObj) {
				cateObj.count++
				cateObj.selected = true
				return
			}
			Vue.set(shopItem, 'count', 1)
			Vue.set(shopItem, 'selected', true)
			state.cartList.push(shopItem)
			uni.showToast({
				title:'成功'
			})
		}
	},
	actions: {

	},
	getters: {
		// totalNum总件数
		totalNum(state) {
			return state.cartList.reduce((pre, item) => {
				if (item.selected) {
					pre += item.count
				}
				return pre
			}, 0)
		},
		// 总价格
		totalPrice(state) {
			return state.cartList.reduce((pre, item) => {
				if (item.selected) {
					pre += item.count * item.retailPrice
				}
				return pre
			}, 0)
		},
		// 是否全选
		isAllSelected(state) {
			return state.cartList.every(item => item.selected)
		}
	}
}
